<template>
	<view style="padding: 30rpx;">
		<abnor v-if="card.list.length<=0"></abnor>
		<view class="list" v-else>
			<scroll-view :scroll-top="scrollTop" scroll-y="true" class="scroll-Y" @scrolltolower="scroll">
				<view class="item" v-for="(item,index) in card.list" :key="item.id">
					<view class="box">
						<view class="image">
							<image :src="item.image" mode="aspectFill"></image>
						</view>
						<view class="info">
							<view class="left">
								<view class="title">
									{{item.goods}}
								</view>
								<checkbox-group @change="checkboxChangs(index,item.id)">
									<label>
										<checkbox :value="item.status" :checked="item.status" style="transform:scale(0.7);" />
									</label>
								</checkbox-group>
							</view>C
							<view class="" style="display: flex;align-items: center;justify-content: space-between;">
								<view class="price">
									￥{{item.unit_price}}
								</view>
								<view class="right">
									<view class="jian" @click="jian(item.id)">
										<u-icon name="minus" color="#2979ff" size="12"></u-icon>
									</view>
									<view class="input">{{item.num}}</view>
									<view class="jia" @click="jia(item.id)">
										<u-icon name="plus" color="#2979ff" size="12"></u-icon>
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
		<view class="" style="height: 100rpx;"></view>
		<view class="bottom">
			<view class="tolit">
				<!-- <checkbox-group @change="checkboxChange">
					<label>
						<checkbox :value="status" :checked="status" style="transform:scale(0.7)" />全选
					</label>
				</checkbox-group> -->
				<!-- <view class="shuju"> -->
				<view class="tPrice font36">
					￥{{card.total_price}}
				</view>
				<view class="btn">
					<view class="deletes font36" @click="deletes" v-if="deleteShow">
						删除
					</view>
					<view class="jiesuan font36" @click="jiesuan">
						结算
					</view>
				</view>
				<!-- </view> -->
			</view>
		</view>
	</view>
</template>

<script>
import index from '../../api'
	export default {
		data() {
			return {
				card: {
					list: [],
					total_price: 0
				},
				scrollTop: 0,
				status: false,
				deleteShow: false
			}
		},
		onLoad() {
			this.getData()
		},
		methods: {
			async getData() {
				const data = await this.$api.zxcadd.shopGoodsCarList()
				data.list.map(item => {
					item.status = false
				})
				this.card.list = data.list
			},
			scroll() {

			},
			async jian(id) {
				const data = await this.$api.zxcadd.handleShopGoodsCar({
					type: 2,
					id: id
				})
				if (data == '操作成功') {
					this.getData();
					this.card.list.map(item => {
						if (item.id == id) {
							item.num = Number(item.num) - 1
						}
					})
				}

			},
			async jia(id) {
				const data = await this.$api.zxcadd.handleShopGoodsCar({
					type: 1,
					id: id
				})
				if (data == '操作成功') {
					this.card.list.map(item => {
						if (item.id == id) {
							item.num = Number(item.num) + 1
						}
					})
				}
			},
			checkboxChangs(e,id) {
				console.log(e,id);
				this.card.list.map(item => {
					if(item.id == id){
						item.status = !item.status
					}else{
						item.status = false
					}
					
				})
				// this.card.list[e].status = !this.card.list[e].status
				// this.card.list[e].status = !this.card.list[e].status
				// this.card.list.map(item => {
				// 	if (item.status) {
				// 		this.deleteShow = true
				// 	}else{
				// 		// this.deleteShow = false
				// 	}
				// })
				if(this.card.list[e].status){
					this.deleteShow = true
					// this.card.total_price = Number(this.card.list[e].unit_price) * Number(this.card.list[e].num)
					this.card.total_price = this.card.list[e].price
				}else{
					this.deleteShow = false
					this.card.total_price = 0
				}
				// this.card.list.map(item => {
				// 	if (!item.status) {
				// 		this.status = false
				// 	} else {
				// 		this.status = true
				// 	}
				// })

				console.log(e, this.status, this.card.list);
			},
			checkboxChange(e) {
				this.status = !this.status
				if (this.status) {
					let tolit = 0
					this.card.list.map(item => {
						item.status = true
						tolit = tolit + Number(item.unit_price) * Number(item.num)
					})
					this.card.total_price = tolit
					this.deleteShow = true
				} else {
					this.card.list.map(item => {
						item.status = false
					})
					this.card.total_price = 0
					this.deleteShow = false
				}
				console.log(e, this.status, this.card.list);
			},
			deletes() {
				uni.showModal({
					title: '提示',
					content: '确定删除此商品？',
					success: (res) => {
						if (res.confirm) {
							let list = []
							this.card.list.map(item => {
								if (item.status) {
									list.push(item.id)
								}
							})
							const id = list.join(',')
							this.$api.zxcadd.delShopGoodsCar({
								id: id
							}).then(resw => {
								console.log(resw,'resw');
								uni.showToast({
									title:resw,
									icon:'success',
									duration:1000
								})
							})
							this.deleteShow = false
							this.card.total_price = 0
							this.card.list.map(item => {
								item.status = false
								
							})
							this.getData()
						} else if (res.cancel) {
						}
					}
				});
			},
			jiesuan() {
				let id = 0
				let gid = 0
				let num = 0
				this.card.list.map(item => {
					if (item.status) {
						id = item.goods_id
						gid = item.id
						num = item.num
					}
				})
				if(id == 0){
					uni.showToast({
						icon:'error',
						title:"请选择一个产品"
					})
					return
				}
				uni.navigateTo({
					url: '/user/pages/shopOrder?id=' + id +'&gid=' + gid + '&num=' + num
				})
			}
		}
	}
</script>

<style scoped>
	.list {
		padding: 0 16rpx;
		border-radius: 8rpx;
		background-color: #fff;
	}

	.scroll-Y {}

	.item {
		display: flex;
		align-items: center;
		padding: 16rpx 0;
		border-bottom: 2rpx solid #ddd;
	}

	.box {
		display: flex;
		align-items: center;
		width: 100%;
		margin-left: 16rpx;
	}

	.image {
		width: 300rpx;
		height: 300rpx;
		border-radius: 16rpx;
		overflow: hidden;
		margin-right: 24rpx;
	}

	image {
		width: 100%;
		height: 100%;
	}

	.info {
		flex: 1;
		display: flex;
		justify-content: space-between;
		height: 300rpx;
		flex-direction: column;
	}

	.left {
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.title {
		font-size: 32rpx;
		font-weight: bold;
	}

	.price {
		color: red;
	}

	.right {
		display: flex;
		align-items: flex-end;
	}

	.jian {
		width: 42rpx;
		height: 42rpx;
		border: 1px solid #2979ff;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.jia {
		width: 42rpx;
		height: 42rpx;
		border: 1px solid #2979ff;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.input {
		width: 48rpx;
		height: 42rpx;
		padding: 0 6rpx;
		font-size: 28rpx;
		text-align: center;
	}

	.bottom {
		position: fixed;
		bottom: 0;
		width: 100%;
		transform: translateX(-30rpx);
		padding: 24rpx 30rpx;
		background-color: #fff;
	}

	.shuju {
		display: flex;
		align-items: center;
	}

	.tPrice {
		color: red;
		margin-right: 16rpx;
	}

	.btn {
		display: flex;
		align-items: center;
	}

	.jiesuan {
		height: 60rpx;
		width: 128rpx;
		background-color: #1DB481;
		color: #fff;
		border-radius: 50rpx;
		text-align: center;
		line-height: 60rpx;
	}

	.deletes {
		height: 60rpx;
		width: 128rpx;
		background-color: red;
		color: #fff;
		border-radius: 50rpx;
		text-align: center;
		line-height: 60rpx;
		margin-right: 24rpx;
	}

	.tolit {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}
	checkbox .wx-checkbox-input,checkbox .uni-checkbox-input{
		background-color: #eee !important;
	}
</style>